﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using ScrumTable.UI.View.Util;

namespace ScrumTable.UI.View.WinTouch.UserControls
{
	/// <summary>
	/// Interaction logic for Marker.xaml
	/// </summary>
	public partial class Marker
	{

        /// <summary>
        /// DependencyProperty for MarkerControlProperty.
        /// </summary>
        public static readonly DependencyProperty MarkerControlProperty =
            DependencyProperty.Register("MarkerControl",
                                        typeof(Control),
                                        typeof(Marker),
                                        new UIPropertyMetadata(null));
        /// <summary>
        /// Marks this Control
        /// </summary>
        /// <value>The control.</value>
        public Control MarkerControl
        {
            get { return (Control)GetValue(MarkerControlProperty); }
            set { SetValue(MarkerControlProperty, value); }
        }

        public Marker()
		{
		    InitializeComponent();
           ((Storyboard)Resources["Effekt"]).Begin(this, HandoffBehavior.Compose);
           this.Loaded += new RoutedEventHandler(OnMarkerLoaded);
		}

        void OnMarkerLoaded(object sender, RoutedEventArgs e)
        {
         
            var parent =  ViewElementUtils.FindVisualParent<ScrumUserControl>(this);
            Loaded -= OnMarkerLoaded;
            if(Margin.Top == 0 || Margin.Left == 0)
            {
                Visibility = Visibility.Hidden;
                Content = null;
                return;
            }
            if (parent == null)
            {
                return;
            }

            //TODO relative position...
            /*            
            double backGroundHeight = ((ImageBrush) ((Grid) parent.Content).Background).ImageSource.Height;
            double backgroundWidth = ((ImageBrush)((Grid)parent.Content).Background).ImageSource.Width;

            double sHeight = (parent.ActualHeight-backGroundHeight)/2;
            double sWidth = (parent.ActualWidth-backgroundWidth)/2;
            var x = backGroundHeight * (sHeight-1) /2;
            var y = backgroundWidth * (sWidth - 1) /2;*/
            Margin = new Thickness(Margin.Left * parent.ActualWidth / 1014.0,Margin.Top * parent.ActualHeight / 686, 0, 0);
            
        }

        protected override void OnPreviewTouchDown(System.Windows.Input.TouchEventArgs e)
        {
            CenterCircle.Fill = path_Copy1.Fill = Brushes.Blue;
            base.OnPreviewTouchDown(e);
        }

        protected override void OnPreviewMouseDown(System.Windows.Input.MouseButtonEventArgs e)
        {
            CenterCircle.Fill = path_Copy1.Fill = Brushes.Blue;
            base.OnPreviewMouseDown(e);
        }

        protected override void OnTouchUp(System.Windows.Input.TouchEventArgs e)
        {
            CenterCircle.Fill = Brushes.Black;
            path_Copy1.Fill = Brushes.Red;
            base.OnTouchUp(e);
        }

        protected override void OnMouseUp(System.Windows.Input.MouseButtonEventArgs e)
        {
            CenterCircle.Fill = Brushes.Black;
            path_Copy1.Fill = Brushes.Red;
            base.OnMouseUp(e);
        }


        protected override void OnMouseLeave(System.Windows.Input.MouseEventArgs e)
        {
            CenterCircle.Fill = Brushes.Black;
            path_Copy1.Fill = Brushes.Red;
            base.OnMouseLeave(e);
        }

        protected override void OnTouchLeave(System.Windows.Input.TouchEventArgs e)
        {
            CenterCircle.Fill = Brushes.Black;
            path_Copy1.Fill = Brushes.Red;
            base.OnTouchLeave(e);
        }

	    private void MarkerTouchEnter(object sender, System.Windows.Input.TouchEventArgs e)
        {
            ((Storyboard)Resources["Effekt"]).Begin(this, HandoffBehavior.Compose);
        }

        private void OnMouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            ((Storyboard)Resources["Effekt"]).Begin(this, HandoffBehavior.Compose);
        }
	}
}